﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using WenSkin.Controls;

namespace WenFrame
{
    public partial class LoginForm : WenSkin.Forms.WenForm
    {
        public LoginForm()
        {
            InitializeComponent();
            ReUI();
            foreach (var item in wenLableTextBox2.Controls)
            {
                if (item is WenTextBox textBox)
                {
                    textBox.KeyDown += (s, e) =>
                    {
                        if (e.KeyCode == Keys.Enter)
                        {
                            CheckStart();
                        }
                    };
                }
            }
        }

        #region 公有属性 

        [Category("Wen"), Description("软件底部信息描述"), DefaultValue("软件快速开发框架")]
        public string SwInfo { get; set; } = "软件快速开发框架";

        #endregion

        #region 界面刷新出现动态效果

        private void ReUI()
        {
            Task.Run(() =>
            {

                while (true)
                {
                    if (this.IsDisposed)
                        return;

                    System.Threading.Thread.Sleep(500);
                    this.Invalidate();
                }
            });
        }

        #endregion

        protected override void OnShown(EventArgs e)
        {
            base.OnShown(e);
            label1.Text = "";
            Satrt();
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);


            Graphics g = e.Graphics.SetGDIHigh();
            Pen pen = new Pen(Color.FromArgb(45, 45, 48), 3);

            for (int i = 0; i < 15; i++)
            {
                Random ran = new Random(Guid.NewGuid().GetHashCode());
                int v1 = ran.Next(10, 450);
                int v2 = ran.Next(30, 260);
                g.DrawEllipse(pen, new Rectangle(v1, v2, 100, 100));
            }

            //g.DrawEllipse(pen, new Rectangle(10, 50, 100, 100));
            //g.DrawEllipse(pen, new Rectangle(30, 130, 100, 100));
            //g.DrawEllipse(pen, new Rectangle(-20, 200, 100, 100));

            g.DrawString(this.SwInfo, new Font("微软雅黑", 20), new SolidBrush(Color.FromArgb(100, 100, 100)), new Rectangle(260, 260, 400, 200));
        }


        private readonly WenSkin.WenConfig config = new WenSkin.WenConfig();
        private readonly WenSkin.SQL.SqlConnect SqlConnect = new WenSkin.SQL.SqlConnect();

        private void Satrt()
        {
            wenLableTextBox1.Text = config["UserConfig", "UserName"];
            if (config["UserConfig", "SavePassword"].ToUpper() == "TRUE")
            {
                checkBox1.Checked = true;
                wenLableTextBox2.Text = config["UserConfig", "Password"];
            }
            if (config["UserConfig", "AutoLogin"].ToUpper() == "TRUE")
            {
                checkBox2.Checked = true;
                CheckStart();
            }
        }

        private void CheckEnd()
        {
            config["UserConfig", "UserName"] = wenLableTextBox1.Text;
            config["UserConfig", "Password"] = wenLableTextBox2.Text;
            config["UserConfig", "SavePassword"] = checkBox1.Checked.ToString();
            config["UserConfig", "AutoLogin"] = checkBox2.Checked.ToString();
            config.Save();
        }

        private void CheckStart()
        {
            if (LoginCheck(wenLableTextBox1.Text, wenLableTextBox2.Text))
            {
                DialogResult = DialogResult.OK;
                this.Dispose();
            }
        }
        private bool LoginCheck(string user, string pad)
        {
            string sql = $"SELECT [Name] ,[Password] FROM [w_user] where Name=N'{user.Replace("'", "''")}' and del=1";
            DataTable dt = SqlConnect.SqlDataTable(sql);
            if (dt.Rows.Count != 0)
            {
                string password = dt.Rows[0]["Password"]?.ToString();
                if (password == pad)
                {
                    label1.Text = "校验成功";
                    CheckEnd();
                    return true;
                }

            }
            label1.Text = "获取用户名或者密码校验失败";
            return false;
        }

        private void wenButton1_Click(object sender, EventArgs e)
        {
            CheckStart();
        }
    }
}
